package cn.systemManage.dao.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import cn.systemManage.bean.UserBean;
import cn.systemManage.dao.UserDao;
import cn.systemManage.dao.handler.ModuleDaoImpl;
@Repository
public class UserDaoImpl extends ModuleDaoImpl<UserBean> implements UserDao{
	/**
	 * 检查用户是否已经存在
	 * @param username
	 * @param id   >0  表示已经存在
	 * @return
	 * @throws Exception
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午02:03:30
	 */
	public int checkIsExsit(String username,int id)throws Exception{
		Long result = 1l;
		if(username!=null){
			String hql = "select count(*) from UserBean where username=? and id!=?";
			result = (Long) this.getSession().createQuery(hql).setParameter(0, username).setParameter(1, id).uniqueResult();
		}
		return result.intValue();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午05:06:03
	 */
	public int deleteUserBeanById(int id)throws Exception{
		String hql = "delete from UserBean where id=?";
		return this.getSession().createQuery(hql).setParameter(0, id).executeUpdate();
	}
	public static void main(String[] args) throws Exception {
		String [] springs = new String[]{
				"spring.xml"
		};
		ApplicationContext app = new ClassPathXmlApplicationContext(springs);
		UserDao userDao = (UserDao) app.getBean("userDaoImpl");
		userDao.checkIsExsit("12",1);
	}
	
}
